package com.che.software.testato.business;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.che.software.testato.business.exception.ActionCreationManagerException;
import com.che.software.testato.business.exception.ActionSearchManagerException;
import com.che.software.testato.domain.dao.IActionDAO;
import com.che.software.testato.domain.dao.jdbc.exception.ActionCreationDAOException;
import com.che.software.testato.domain.dao.jdbc.exception.ActionSearchDAOException;
import com.che.software.testato.domain.entity.Intention;
import com.che.software.testato.domain.entity.Section;

/**
 * Manager dedicated to the actions.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @since July, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
@Service("actionManager")
public class ActionManager {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(ActionManager.class);

	/**
	 * Members.
	 */
	@Autowired
	private IActionDAO actionDAO;

	/**
	 * Creates a map from an action plan id, a list of intentions and a list of
	 * sections.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param actionPlanId the action plan id to be linked to the created
	 *        actions.
	 * @param intentions the intentions to add in the map.
	 * @param sections the sections to add in the map.
	 * @since July, 2011.
	 * @throws ActionCreationManagerException if an error occurs during the
	 *         creation.
	 */
	public void createMap(int actionPlanId, List<Intention> intentions, List<Section> sections) throws ActionCreationManagerException {
		LOGGER.debug("createMap(" + actionPlanId + "," + intentions.size() + " intentions," + sections.size() + " sections).");
		try {
			actionDAO.createMap(actionPlanId, intentions, sections);
		} catch (ActionCreationDAOException e) {
			throw new ActionCreationManagerException(e);
		}
	}

	/**
	 * Checks if some action have been setted for a given action plan. In other
	 * words, is able to say if a map has already been setted or not.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param actionPlanId the action plan id.
	 * @return true if the map has already been setted, else false.
	 * @since July, 2011.
	 * @throws ActionSearchManagerException if an error occurs during the
	 *         search.
	 */
	public boolean isActionExistingFromActionPlanId(int actionPlanId) throws ActionSearchManagerException {
		LOGGER.debug("isActionExistingFromActionPlanId(" + actionPlanId + ").");
		try {
			return actionDAO.isActionExistingFromActionPlanId(actionPlanId);
		} catch (ActionSearchDAOException e) {
			throw new ActionSearchManagerException(e);
		}
	}

	/**
	 * Getter for the private field value actionDAO.
	 * 
	 * @return the actionDAO field value.
	 */
	public IActionDAO getActionDAO() {
		return actionDAO;
	}

	/**
	 * Setting a value to the actionDAO field.
	 * 
	 * @param actionDAO the value to set.
	 */
	public void setActionDAO(IActionDAO actionDAO) {
		this.actionDAO = actionDAO;
	}
}